<script type="text/javacript">
    jQuery.fn.multiselect = function() {
    $(this).each(function() {
    var checkboxes = $(this).find("input:checkbox");
    checkboxes.each(function() {
    var checkbox = $(this);
    // Highlight pre-selected checkboxes
    if (checkbox.attr("checked"))
    checkbox.parent().addClass("multiselect-on");

    // Highlight checkboxes that the user selects
    checkbox.click(function() {
    if (checkbox.attr("checked"))
    checkbox.parent().addClass("multiselect-on");
    else
    checkbox.parent().removeClass("multiselect-on");
    });
    });
    });
    };

    $(function() {
    $(".multiselect").multiselect();
    });
</script>
<div id="breadcrumbs"><?php echo $this->Html->link('Home', array('controller' => 'contents', 'action' => 'view', 'home')); ?> > Assign Roster
    </div>
<?php
echo $this->Form->create('adminrosters', array('action' => 'save'));
//display next month for assigning
$month = $this->Time->format('m', $timeset);
$monthyear = $this->Time->format('Y-m', $timeset);
?>

<div class="month">
    <?php
    echo "<table>";
    echo "<tr>";
    echo "<th>";
    if ($timeset != "-1 month") {
        echo $this->Html->link('<<', array('action' => 'assign', 0)) . " ";
    }
    echo "</th>";

    echo "<th>";
    if ($timeset == "+1 month") {
        echo $this->Html->link('<', array('action' => 'assign', 1));
    }
    echo "</th>";

    echo "<th>";
    echo "<h2><b>" . $this->Time->format('F', $timeset) . "</b></h2>";
    echo "</th>";

    echo "<th>";
    if ($timeset == "-1 month") {
        echo $this->Html->link('>', array('action' => 'assign', 1));
    }
    echo "</th>";

    echo "<th>";
    if ($timeset != "+1 month") {
        echo $this->Html->link('>>', array('action' => 'assign', 2));
    }
    echo "</th></style>";

//debug($this->Time->format('m', $timeset));
    echo "</tr>";
    echo "</table>";
    ?>
</div>

<table>
    <tr>
        <th>Monday</th>
        <th>Tuesday</th>
        <th>Wednesday</th>
        <th>Thursday</th>
        <th>Friday</th>
    </tr>

    <?php
    $startdate = $this->Time->format('l', $monthyear . '-1');

//creating calendar view
    if ($startdate == "Monday") {
        echo "<tr>";
    } else if ($startdate == "Tuesday") {
        echo "<tr><td>";
    } else if ($startdate == "Wednesay") {
        echo "<tr><td><td>";
    } else if ($startdate == "Thursday") {
        echo "<tr><td><td><td>";
    } else if ($startdate == "Friday") {
        echo "<tr><td><td><td><td>";
    }

    for ($n = 1; $n <= $this->Time->format('t', $month); $n++) {
        $day = $this->Time->format('l', $monthyear . "-" . $n);

        if (($day != "Saturday") && ($day != "Sunday")) {
            echo "<td><b>" . $n . "</b>";
            //the other codes should be here

            foreach ($times as $timing):
                echo "<br>";
                echo "<b>" . $this->Time->format('H:i', $timing['Time']['time_from']) . " - " . $this->Time->format('H:i', $timing['Time']['time_to']) . "</b> \n";
                echo '<div class="multiselect">';
                foreach ($rosters as $roster):
                    if ($this->Time->format('j', $roster['Roster']['ros_date']) == $n && $this->Time->format('Y-m', $roster['Roster']['ros_date']) == $monthyear) {
                        if ($timing['Time']['id'] == $roster['Roster']['time_id']) {
                            foreach ($users as $user):
                                if ($user['User']['user_id'] == $roster['Roster']['ros_userid']) {
                                    if ($roster['Roster']['isapprove'] == 0) {
                                        echo '<label><input style="font-weight:normal" type="checkbox" name="option[]" value="' . $roster['Roster']['ros_id'] . '" />' . $user['User']['staff_name'] . '</label>';
                                        
                                    } else {
                                        echo '<label><input style="font-weight:normal" type="checkbox"  name="option[]" value="' . $roster['Roster']['ros_id'] . '" checked="checked"/>' . $user['User']['staff_name'] . ' </label>';
                                      
                                    }
                                }
                            endforeach;
                        }
                    }

                endforeach;
                ?>
                <!--<a data-toggle="modal" href="#myModal" class="btn btn-primary btn-lg">Click here to assign</a>-->

                <?php
                
                echo $this->Html->link('Assign Staff', array('controller' => 'Adminrosters', 'action' => 'staff', '?' => array('time' => $timing['Time']['id'], 'date' => $monthyear . "-" . $n)));

                echo '</div>';
            endforeach;

            echo "</td>";
            if ($day == "Friday") {
                echo "</tr>";
            }
        }
    }
    ?>
</table>

<?php
echo $this->Form->hidden('monthyear',array('value'=>$monthyear));
echo $this->Form->end('Submit');
?>

<br>